package com.fcscs.ecms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fcscs.core.service.PropertyManager;
import com.fcscs.cservice.communication.OutboundHandler;
import com.fcscs.ecms.dao.ECMSCommonDao;
import com.fcscs.ecms.domain.GlobalHotelEntity;
import com.fcscs.ecms.domain.GlobalHotelRoomTypeList;
import com.fcscs.ecms.domain.GlobalHotelRoomTypeListId;
import com.fcscs.ecms.domain.GlobalLanguage;
import com.fcscs.ecms.domain.GlobalModuleInfo;
import com.fcscs.ecms.domain.GlobalModuleType;
import com.fcscs.ecms.domain.GlobalModuleType.ModuleType;
import com.fcscs.ecms.domain.GlobalPicture;
import com.fcscs.ecms.domain.GlobalProprietaryInfo;
import com.fcscs.ecms.domain.GlobalRoomType;
import com.fcscs.ecms.domain.GlobalText;
import com.fcscs.ecms.domain.HotelEntityView;
import com.fcscs.ecms.domain.MdlEntertainmentDynamicField;
import com.fcscs.ecms.domain.MdlEntertainmentElmts;
import com.fcscs.ecms.domain.MdlEntertainmentMain;
import com.fcscs.ecms.domain.MdlHelpCategory;
import com.fcscs.ecms.domain.MdlHelpMain;
import com.fcscs.ecms.domain.MdlHelpMainId;
import com.fcscs.ecms.domain.ScheduleActivation;
import com.fcscs.ecms.domain.ScheduleActivationId;
import com.fcscs.ecms.service.ECMSCommonManager;

@Service("ecmsCommonManager")
public class ECMSCommonManagerImpl implements ECMSCommonManager {

	@Autowired
	private ECMSCommonDao ecmsCommonDao;

	@Autowired
	PropertyManager propMgr;

	@Autowired
	OutboundHandler outHdl;

	@SuppressWarnings("unused")
	private static Logger LOGGER = Logger
			.getLogger(ECMSCommonManagerImpl.class);

	@Override
	public GlobalLanguage saveGlobalLanguage(GlobalLanguage entity) {
		return ecmsCommonDao.saveGlobalLanguage(entity);
	}

	@Override
	public GlobalText saveGlobalText(GlobalText entity) {
		return ecmsCommonDao.saveGlobalText(entity);
	}

	@Override
	public GlobalPicture saveGlobalPicture(GlobalPicture entity) {
		return ecmsCommonDao.saveGlobalPicture(entity);
	}

	@Override
	public GlobalPicture updateGlobalPicture(GlobalPicture entity) {
		// TODO Auto-generated method stub
		GlobalPicture picture = ecmsCommonDao.getImage(entity.getId()).get(0);
		picture.setBinaryData(entity.getBinaryData());
		picture.setVersion(entity.getVersion());
		return ecmsCommonDao.updateGlobalPicture(picture);
	}

	@Override
	public GlobalHotelEntity saveGlobalHotelEntity(GlobalHotelEntity entity) {
		// TODO Auto-generated method stub
		return ecmsCommonDao.saveGlobalHotelEntity(entity);
	}

	@Override
	public List<GlobalRoomType> getRoomType() {
		// TODO Auto-generated method stub
		return ecmsCommonDao.getRoomType();
	}

	@Override
	public List<String> getText(int id, String langId) {
		// TODO Auto-generated method stub
		return ecmsCommonDao.getText(id, langId);
	}

	@Override
	public List<Integer> getHotelRoomTypeList(int idHotel) {
		// TODO Auto-generated method stub
		return ecmsCommonDao.getHotelRoomTypeList(idHotel);
	}

	@Override
	public List<GlobalPicture> getImage(int id) {
		// TODO Auto-generated method stub
		return ecmsCommonDao.getImage(id);
	}

	@Override
	public List<GlobalHotelEntity> getHotel(int id) {
		// TODO Auto-generated method stub
		return ecmsCommonDao.getHotel(id);
	}

	@Override
	public List<GlobalHotelEntity> getHotels() {
		// TODO Auto-generated method stub
		return ecmsCommonDao.getHotels();
	}

	@Override
	public HotelEntityView getHotelEntityView(int idHotel) {
		// TODO Auto-generated method stub

		HotelEntityView entity = ecmsCommonDao.getHotelEntityView(idHotel);
		entity.setNameTextMap(ecmsCommonDao.getGlobalTextMap(entity
				.getNameRef()));
		entity.setdescTextMap(ecmsCommonDao.getGlobalTextMap(entity
				.getDescRef()));

		return entity;
	}

	@Override
	public boolean deleteHotelRoomTypeList(int idHotel,
			List<Integer> roomTypeList) {
		// TODO Auto-generated method stub

		return ecmsCommonDao.deleteHotelRoomTypeList(idHotel, 0);
	}

	@Override
	public boolean deleteHotel(int idHotel) {
		// TODO Auto-generated method stub
		return ecmsCommonDao.deleteHotel(idHotel);
	}

	private int saveProprietaryInfo(int orgId, int propId, int userId) {
		Date now = new Date();
		GlobalProprietaryInfo propInfo = new GlobalProprietaryInfo();
		propInfo.setOrgId(orgId);
		propInfo.setPropId(propId);
		propInfo.setCreated(now);
		propInfo.setCreatedBy(userId);
		return ecmsCommonDao.saveGlobalProprietaryInfo(propInfo);
	}

	private void updateProprietaryInfo(int propInfoId, int userId) {
		Date now = new Date();
		GlobalProprietaryInfo propInfo = ecmsCommonDao
				.getGlobalProprietaryInfo(propInfoId);
		propInfo.setUpdated(now);
		propInfo.setUpdatedBy(userId);
		ecmsCommonDao.updateGlobalProprietaryInfo(propInfo);
	}

	@Override
	public MdlEntertainmentMain getEntertainment(int orgId, int propId,
			boolean isSchedule) {
		// Get entertainment main
		MdlEntertainmentMain entMain = ecmsCommonDao.getEntertainmentMain(
				orgId, propId, isSchedule);
		if (entMain != null) {
			// Get module info
			GlobalModuleInfo moduleInfo = ecmsCommonDao
					.getGlobalModuleInfo(entMain.getId());
			moduleInfo.setTitleTextMap(ecmsCommonDao
					.getGlobalTextMap(moduleInfo.getTitleRef()));
			moduleInfo.setSubTitleTextMap(ecmsCommonDao
					.getGlobalTextMap(moduleInfo.getSubTitleRef()));

			// Get schedule activation if is schedule screen
			if (isSchedule) {
				ScheduleActivation sche = ecmsCommonDao
						.getScheduleActivation(new ScheduleActivationId(
								moduleInfo.getId(), ModuleType.ENTERTAINMENT
										.getId()));
				moduleInfo.setScheduleActivation(sche);
			}

			entMain.setModuleInfo(moduleInfo);

			// Get ent elmts
			List<MdlEntertainmentElmts> entElmtsList = ecmsCommonDao
					.getEntertainmentElmtsList(entMain.getId());
			MdlEntertainmentDynamicField entDynamicField;
			for (MdlEntertainmentElmts entElmts : entElmtsList) {
				entDynamicField = ecmsCommonDao
						.getEntertainmentDynamicField(entElmts.getFieldId());
				entDynamicField.setDescTextMap(ecmsCommonDao
						.getGlobalTextMap(entDynamicField.getDescRef()));
				entElmts.setDynamicField(entDynamicField);
			}
			entMain.setEntertainmentElmts(entElmtsList);
		}
		return entMain;
	}

	@Override
	public MdlEntertainmentMain getEntertainment(int entMainId) {
		// Get entertainment main
		MdlEntertainmentMain entMain = ecmsCommonDao
				.getEntertainmentMain(entMainId);
		if (entMain != null) {
			// Get module info
			GlobalModuleInfo moduleInfo = ecmsCommonDao
					.getGlobalModuleInfo(entMain.getId());
			moduleInfo.setTitleTextMap(ecmsCommonDao
					.getGlobalTextMap(moduleInfo.getTitleRef()));
			moduleInfo.setSubTitleTextMap(ecmsCommonDao
					.getGlobalTextMap(moduleInfo.getSubTitleRef()));

			// Get schedule activation if is schedule screen
			if (moduleInfo.isIsScheduleScreen()) {
				ScheduleActivation sche = ecmsCommonDao
						.getScheduleActivation(new ScheduleActivationId(
								moduleInfo.getId(), ModuleType.ENTERTAINMENT
										.getId()));
				moduleInfo.setScheduleActivation(sche);
			}

			entMain.setModuleInfo(moduleInfo);

			// Get ent elmts
			List<MdlEntertainmentElmts> entElmtsList = ecmsCommonDao
					.getEntertainmentElmtsList(entMain.getId());
			MdlEntertainmentDynamicField entDynamicField;
			for (MdlEntertainmentElmts entElmts : entElmtsList) {
				entDynamicField = ecmsCommonDao
						.getEntertainmentDynamicField(entElmts.getFieldId());
				entDynamicField.setDescTextMap(ecmsCommonDao
						.getGlobalTextMap(entDynamicField.getDescRef()));
				entElmts.setDynamicField(entDynamicField);
			}
			entMain.setEntertainmentElmts(entElmtsList);

			return entMain;
		}
		return null;
	}

	@Override
	public List<MdlEntertainmentDynamicField> getEntertainmentDynamicFieldList() {
		List<MdlEntertainmentDynamicField> dynamicFieldList = ecmsCommonDao
				.getEntertainmentDynamicFieldList();
		for (MdlEntertainmentDynamicField field : dynamicFieldList) {
			field.setDescTextMap(ecmsCommonDao.getGlobalTextMap(field
					.getDescRef()));
		}
		return dynamicFieldList;
	}

	@Override
	public Map<String, String> getLanguageMap() {
		return ecmsCommonDao.getLanguageMap();
	}

	@Override
	public MdlEntertainmentMain saveEntertainment(int orgId, int propId,
			int userId, boolean isSchedule, Date startDate, Date startTime,
			Map<String, String> titleTextMap,
			Map<String, String> subTitleTextMap, byte[] menuPictureData,
			String url, List<MdlEntertainmentElmts> entElmtsList) {

		MdlEntertainmentMain entMain = ecmsCommonDao.getEntertainmentMain(
				orgId, propId, isSchedule);
		if (entMain == null) {
			// Save GlobalModuleInfo
			GlobalModuleInfo moduleInfo = new GlobalModuleInfo();
			moduleInfo.setTypeId(GlobalModuleType.ModuleType.ENTERTAINMENT
					.getId());
			moduleInfo.setProprietaryId(saveProprietaryInfo(orgId, propId,
					userId));
			moduleInfo.setTitleRef(ecmsCommonDao
					.saveGlobalTextMap(titleTextMap));
			moduleInfo.setSubTitleRef(ecmsCommonDao
					.saveGlobalTextMap(subTitleTextMap));
			moduleInfo.setIsScheduleScreen(isSchedule);

			// GlobalPicture
			GlobalPicture menuPicture = new GlobalPicture();
			menuPicture.setProprietaryId(saveProprietaryInfo(orgId, propId,
					userId));
			menuPicture.setBinaryData(menuPictureData != null ? menuPictureData
					: new byte[0]);
			menuPicture.setVersion(1);
			moduleInfo.setMenuPictureRef(ecmsCommonDao.saveGlobalPicture(
					menuPicture).getId());
			int moduleInfoId = ecmsCommonDao.saveGlobalModuleInfo(moduleInfo);

			if (isSchedule) {
				ScheduleActivation sche = new ScheduleActivation();
				sche.setId(new ScheduleActivationId(moduleInfoId,
						GlobalModuleType.ModuleType.ENTERTAINMENT.getId()));
				sche.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
				sche.setIsActivated(false);
				sche.setIsObsoleted(false);
				sche.setActivationDate(startDate);
				sche.setActivationTime(startTime);
				ecmsCommonDao.saveScheduleActivation(sche);
			}

			// Save MdlEntertainmentMain
			entMain = new MdlEntertainmentMain();
			entMain.setId(moduleInfoId);
			entMain.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
			entMain.setUrl(url);
			entMain.setEntertainmentElmts(entElmtsList);
			int entMainId = ecmsCommonDao.saveEntertainmentMain(entMain);
			entMain = ecmsCommonDao.getEntertainmentMain(entMainId);
			return entMain;
		} else {
			int entMainId = entMain.getId();
			// Update GlobalModuleInfo
			GlobalModuleInfo moduleInfo = ecmsCommonDao
					.getGlobalModuleInfo(entMainId);
			updateProprietaryInfo(moduleInfo.getProprietaryId(), userId);
			ecmsCommonDao.updateGlobalTextMap(moduleInfo.getTitleRef(),
					titleTextMap);
			ecmsCommonDao.updateGlobalTextMap(moduleInfo.getSubTitleRef(),
					subTitleTextMap);
			ecmsCommonDao.updateGlobalModuleInfo(moduleInfo);

			// Update GlobalPicture
			if (menuPictureData != null) {
				GlobalPicture menuPicture = ecmsCommonDao
						.getGlobalPicture(moduleInfo.getMenuPictureRef());
				menuPicture.setVersion(menuPicture.getVersion() + 1);
				menuPicture.setBinaryData(menuPictureData);
				updateProprietaryInfo(menuPicture.getProprietaryId(), userId);
				ecmsCommonDao.updateGlobalPicture(menuPicture);
			}

			// ScheduleActivation
			if (isSchedule) {
				ScheduleActivation sche = ecmsCommonDao
						.getScheduleActivation(new ScheduleActivationId(
								entMainId,
								GlobalModuleType.ModuleType.ENTERTAINMENT
										.getId()));
				sche.setActivationDate(startDate);
				sche.setActivationTime(startTime);
				sche.setIsActivated(false);
				sche.setIsObsoleted(false);
				updateProprietaryInfo(sche.getProprietaryId(), userId);
				ecmsCommonDao.updateScheduleActivation(sche);
			} else {
				ScheduleActivation sche = ecmsCommonDao.getScheduleActivation(
						orgId, propId,
						GlobalModuleType.ModuleType.ENTERTAINMENT.getId());
				if (sche != null && sche.isIsActivated()
						&& !sche.isIsObsoleted()) {
					sche.setIsObsoleted(true);
					updateProprietaryInfo(sche.getProprietaryId(), userId);
					ecmsCommonDao.updateScheduleActivation(sche);
				}
			}

			// Update MdlEntertainmentMain
			entMain.setUrl(url);
			updateProprietaryInfo(entMain.getProprietaryId(), userId);
			entMain.setEntertainmentElmts(entElmtsList);
			ecmsCommonDao.updateEntertainmentMain(entMain);
			return entMain;
		}
	}

	@Override
	public void saveEntertainmentTemplate(int orgId, int propId, int userId,
			Map<String, String> titleTextMap,
			Map<String, String> subTitleTextMap, byte[] menuPictureData,
			String url, List<MdlEntertainmentElmts> entElmtsList,
			String templateName) {
		// Save GlobalModuleInfo
		GlobalModuleInfo moduleInfo = new GlobalModuleInfo();
		moduleInfo.setTypeId(GlobalModuleType.ModuleType.ENTERTAINMENT.getId());
		moduleInfo.setIsScheduleScreen(false);
		moduleInfo.setIsTemplate(true);
		moduleInfo.setTemplateName(templateName);
		moduleInfo.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
		moduleInfo.setTitleRef(ecmsCommonDao.saveGlobalTextMap(titleTextMap));
		moduleInfo.setSubTitleRef(ecmsCommonDao
				.saveGlobalTextMap(subTitleTextMap));
		// GlobalPicture
		GlobalPicture menuPicture = new GlobalPicture();
		menuPicture
				.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
		menuPicture.setBinaryData(menuPictureData != null ? menuPictureData
				: new byte[0]);
		menuPicture.setVersion(1);
		moduleInfo.setMenuPictureRef(ecmsCommonDao.saveGlobalPicture(
				menuPicture).getId());
		int moduleInfoId = ecmsCommonDao.saveGlobalModuleInfo(moduleInfo);

		// Save MdlEntertainmentMain
		MdlEntertainmentMain entMain = new MdlEntertainmentMain();
		entMain.setId(moduleInfoId);
		entMain.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
		entMain.setUrl(url);
		entMain.setEntertainmentElmts(entElmtsList);
		ecmsCommonDao.saveEntertainmentMain(entMain);
	}

	@Override
	public GlobalModuleInfo getGlobalModuleInfoTemplateByName(int orgId,
			int propId, ModuleType type, String templateName) {
		return ecmsCommonDao.getGlobalModuleInfoTemplateByName(orgId, propId,
				type, templateName);
	}

	@Override
	public List<GlobalModuleInfo> getGlobalModuleInfoTemplateList(int orgId,
			int propId, ModuleType type) {
		return ecmsCommonDao.getGlobalModuleInfoTemplateList(orgId, propId,
				type);
	}

	@Override
	public int saveGlobalHotelEntity(List<Map<String, String>> listtext,
			GlobalHotelEntity entity, GlobalPicture[] llistPic) {
		// TODO Auto-generated method stub
		int nameRef = ecmsCommonDao.saveGlobalTextMap(listtext.get(0));
		int descRef = ecmsCommonDao.saveGlobalTextMap(listtext.get(1));
		int phonePicRef = ecmsCommonDao.saveGlobalPicture(llistPic[0]).getId();

		int tabletPicRef = ecmsCommonDao.saveGlobalPicture(llistPic[1]).getId();

		// Blah Blah Blah

		entity.setNameRef(nameRef);
		entity.setDescRef(descRef);
		entity.setPhonePicRef(phonePicRef);
		entity.setTabletPicRef(tabletPicRef);

		int id = ecmsCommonDao.saveGlobalHotelEntity(entity).getId();

		return id;
	}

	@Override
	public int editGlobalHotelEntity(List<Map<String, String>> listtext,
			GlobalHotelEntity entity, GlobalPicture[] llistPic) {
		// TODO Auto-generated method stub

		ecmsCommonDao.updateGlobalTextMap(entity.getNameRef(), listtext.get(0));
		ecmsCommonDao.updateGlobalTextMap(entity.getDescRef(), listtext.get(1));
		updateGlobalPicture(llistPic[0]);
		updateGlobalPicture(llistPic[1]);

		int id = ecmsCommonDao.saveGlobalHotelEntity(entity).getId();

		return id;
	}

	@Override
	public int saveGlobalHotelRoomTypeList(int idHotel,
			List<Integer> listRoomType) {
		// TODO Auto-generated method stub

		for (int i = 0; i < listRoomType.size(); i++) {
			GlobalHotelRoomTypeList entity = new GlobalHotelRoomTypeList();

			entity.setId(new GlobalHotelRoomTypeListId(idHotel, listRoomType
					.get(i)));
			ecmsCommonDao.saveGlobalHotelRoomTypeList(entity);
		}
		return listRoomType.size();
	}

	@Override
	public int editGlobalHotelRoomType(int idHotel,
			List<Integer> oldlistRoomType, List<Integer> newRoomType) {
		// TODO Auto-generated method stub
		for (int idRoom : oldlistRoomType) {
			ecmsCommonDao.deleteHotelRoomTypeList(idHotel, idRoom);
		}

		for (int id : newRoomType) {
			GlobalHotelRoomTypeList entity = new GlobalHotelRoomTypeList();

			entity.setId(new GlobalHotelRoomTypeListId(idHotel, id));
			ecmsCommonDao.saveGlobalHotelRoomTypeList(entity);
		}
		return newRoomType.size();
	}

	@Override
	public int deleteHotelentity(GlobalHotelEntity entity,
			List<Integer> roomTypeList) {
		// TODO Auto-generated method stub
		for (int idRoom : roomTypeList) {
			ecmsCommonDao.deleteHotelRoomTypeList(entity.getId(), idRoom);
		}
		ecmsCommonDao.deleteGlobalText(entity.getNameRef());
		ecmsCommonDao.deleteGlobalText(entity.getPhonePicRef());

		ecmsCommonDao.deleteGlobalPicture(ecmsCommonDao.getImage(
				entity.getPhonePicRef()).get(0));
		ecmsCommonDao.deleteGlobalPicture(ecmsCommonDao.getImage(
				entity.getTabletPicRef()).get(0));

		ecmsCommonDao.deleteHotel(entity.getId());

		return 1;
	}

	@Override
	public GlobalModuleInfo getModuleHelp(int orgId, int propId,
			boolean isSchedule) {
		GlobalModuleInfo moduleInfo = ecmsCommonDao.getHelpModuleInfo(orgId,
				propId, isSchedule);
		if (moduleInfo != null) {
			moduleInfo.setTitleTextMap(ecmsCommonDao
					.getGlobalTextMap(moduleInfo.getTitleRef()));
			moduleInfo.setSubTitleTextMap(ecmsCommonDao
					.getGlobalTextMap(moduleInfo.getSubTitleRef()));
			if (isSchedule) {
				ScheduleActivation sche = ecmsCommonDao
						.getScheduleActivation(new ScheduleActivationId(
								moduleInfo.getId(), ModuleType.HELP.getId()));
				moduleInfo.setScheduleActivation(sche);
			}

			List<MdlHelpMain> helpMainList = ecmsCommonDao
					.getHelpMainList(moduleInfo.getId());
			MdlHelpCategory helpCat;
			for (MdlHelpMain helpMain : helpMainList) {
				helpCat = ecmsCommonDao.getHelpCategory(helpMain.getId()
						.getCategoryId());
				helpCat.setNameTextMap(ecmsCommonDao.getGlobalTextMap(helpCat
						.getNameRef()));
				helpMain.setHelpCategory(helpCat);
			}
			moduleInfo.setHelpMainList(helpMainList);
		}
		return moduleInfo;
	}

	@Override
	public GlobalModuleInfo getModuleHelp(int moduleId) {
		GlobalModuleInfo moduleInfo = ecmsCommonDao
				.getGlobalModuleInfo(moduleId);
		if (moduleInfo != null) {
			moduleInfo.setTitleTextMap(ecmsCommonDao
					.getGlobalTextMap(moduleInfo.getTitleRef()));
			moduleInfo.setSubTitleTextMap(ecmsCommonDao
					.getGlobalTextMap(moduleInfo.getSubTitleRef()));
			if (moduleInfo.isIsScheduleScreen()) {
				ScheduleActivation sche = ecmsCommonDao
						.getScheduleActivation(new ScheduleActivationId(
								moduleInfo.getId(), ModuleType.HELP.getId()));
				moduleInfo.setScheduleActivation(sche);
			}

			List<MdlHelpMain> helpMainList = ecmsCommonDao
					.getHelpMainList(moduleInfo.getId());
			MdlHelpCategory helpCat;
			for (MdlHelpMain helpMain : helpMainList) {
				helpCat = ecmsCommonDao.getHelpCategory(helpMain.getId()
						.getCategoryId());
				helpCat.setNameTextMap(ecmsCommonDao.getGlobalTextMap(helpCat
						.getNameRef()));
				helpMain.setHelpCategory(helpCat);
			}
			moduleInfo.setHelpMainList(helpMainList);
		}
		return moduleInfo;
	}

	@Override
	public GlobalModuleInfo saveModuleHelp(int orgId, int propId, int userId,
			boolean isSchedule, Date startDate, Date startTime,
			Map<String, String> titleTextMap,
			Map<String, String> subTitleTextMap, byte[] menuPictureData,
			List<MdlHelpMain> helpMainList) {
		GlobalModuleInfo moduleInfo = ecmsCommonDao.getHelpModuleInfo(orgId,
				propId, isSchedule);
		if (moduleInfo == null) {
			// Save
			moduleInfo = new GlobalModuleInfo();
			moduleInfo.setTypeId(GlobalModuleType.ModuleType.HELP.getId());
			moduleInfo.setProprietaryId(saveProprietaryInfo(orgId, propId,
					userId));
			moduleInfo.setTitleRef(ecmsCommonDao
					.saveGlobalTextMap(titleTextMap));
			moduleInfo.setSubTitleRef(ecmsCommonDao
					.saveGlobalTextMap(subTitleTextMap));
			moduleInfo.setIsScheduleScreen(isSchedule);

			// GlobalPicture
			GlobalPicture menuPicture = new GlobalPicture();
			menuPicture.setProprietaryId(saveProprietaryInfo(orgId, propId,
					userId));
			menuPicture.setBinaryData(menuPictureData != null ? menuPictureData
					: new byte[0]);
			menuPicture.setVersion(1);
			moduleInfo.setMenuPictureRef(ecmsCommonDao.saveGlobalPicture(
					menuPicture).getId());
			int moduleInfoId = ecmsCommonDao.saveGlobalModuleInfo(moduleInfo);

			if (isSchedule) {
				ScheduleActivation sche = new ScheduleActivation();
				sche.setId(new ScheduleActivationId(moduleInfoId,
						GlobalModuleType.ModuleType.HELP.getId()));
				sche.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
				sche.setIsActivated(false);
				sche.setIsObsoleted(false);
				sche.setActivationDate(startDate);
				sche.setActivationTime(startTime);
				ecmsCommonDao.saveScheduleActivation(sche);
			}

			// Save MdlHelpMain
			MdlHelpCategory helpCat;
			int helpCatId;
			for (MdlHelpMain helpMain : helpMainList) {
				helpCat = helpMain.getHelpCategory();
				helpCat.setProprietaryId(saveProprietaryInfo(orgId, propId,
						userId));
				helpCat.setNameRef(ecmsCommonDao.saveGlobalTextMap(helpCat
						.getNameTextMap()));
				helpCatId = ecmsCommonDao.saveHelpCategory(helpCat);
				MdlHelpMainId helpMainId = new MdlHelpMainId(moduleInfoId, helpCatId);
				helpMain.setId(helpMainId);
				helpMain.setProprietaryId(saveProprietaryInfo(orgId, propId,
						userId));
				ecmsCommonDao.saveHelpMain(helpMain);
			}
		} else {
			// Update GlobalModuleInfo
			updateProprietaryInfo(moduleInfo.getProprietaryId(), userId);
			ecmsCommonDao.updateGlobalTextMap(moduleInfo.getTitleRef(),
					titleTextMap);
			ecmsCommonDao.updateGlobalTextMap(moduleInfo.getSubTitleRef(),
					subTitleTextMap);
			ecmsCommonDao.updateGlobalModuleInfo(moduleInfo);

			// Update GlobalPicture
			if (menuPictureData != null) {
				GlobalPicture menuPicture = ecmsCommonDao
						.getGlobalPicture(moduleInfo.getMenuPictureRef());
				menuPicture.setVersion(menuPicture.getVersion() + 1);
				menuPicture.setBinaryData(menuPictureData);
				updateProprietaryInfo(menuPicture.getProprietaryId(), userId);
				ecmsCommonDao.updateGlobalPicture(menuPicture);
			}

			// ScheduleActivation
			if (isSchedule) {
				ScheduleActivation sche = ecmsCommonDao
						.getScheduleActivation(new ScheduleActivationId(
								moduleInfo.getId(),
								GlobalModuleType.ModuleType.HELP.getId()));
				sche.setActivationDate(startDate);
				sche.setActivationTime(startTime);
				sche.setIsActivated(false);
				sche.setIsObsoleted(false);
				updateProprietaryInfo(sche.getProprietaryId(), userId);
				ecmsCommonDao.updateScheduleActivation(sche);
			} else {
				ScheduleActivation sche = ecmsCommonDao
						.getScheduleActivation(orgId, propId,
								GlobalModuleType.ModuleType.HELP.getId());
				if (sche != null && sche.isIsActivated()
						&& !sche.isIsObsoleted()) {
					sche.setIsObsoleted(true);
					updateProprietaryInfo(sche.getProprietaryId(), userId);
					ecmsCommonDao.updateScheduleActivation(sche);
				}
			}

			// Update MdlHelpMain
			// Delete old
			List<MdlHelpMain> helpMainListOld = ecmsCommonDao
					.getHelpMainList(moduleInfo.getId());
			MdlHelpCategory helpCatOld;
			for (MdlHelpMain helpMainOld : helpMainListOld) {
				helpCatOld = ecmsCommonDao.getHelpCategory(helpMainOld.getId()
						.getCategoryId());
				ecmsCommonDao.deleteGlobalProprietaryInfo(helpMainOld
						.getProprietaryId());
				ecmsCommonDao.deleteGlobalProprietaryInfo(helpCatOld
						.getProprietaryId());
				ecmsCommonDao.deleteHelpCategory(helpCatOld);
				ecmsCommonDao.deleteHelpMain(helpMainOld);
			}
			// Save new
			MdlHelpCategory helpCat;
			int helpCatId;
			for (MdlHelpMain helpMain : helpMainList) {
				helpCat = helpMain.getHelpCategory();
				helpCat.setProprietaryId(saveProprietaryInfo(orgId, propId,
						userId));
				helpCat.setNameRef(ecmsCommonDao.saveGlobalTextMap(helpCat
						.getNameTextMap()));
				helpCatId = ecmsCommonDao.saveHelpCategory(helpCat);
				helpMain.getId().setCategoryId(helpCatId);
				helpMain.getId().setId(moduleInfo.getId());
				helpMain.setProprietaryId(saveProprietaryInfo(orgId, propId,
						userId));
				ecmsCommonDao.saveHelpMain(helpMain);
			}
		}
		return moduleInfo;
	}
	
	@Override
	public void saveHelpTemplate(int orgId, int propId, int userId,
			Map<String, String> titleMap, Map<String, String> subTitleMap,
			byte[] menuPictureData, List<MdlHelpMain> helpMainList,
			String templateName) {

		// Save GlobalModuleInfo
		GlobalModuleInfo moduleInfo = new GlobalModuleInfo();
		moduleInfo.setTypeId(GlobalModuleType.ModuleType.HELP.getId());
		moduleInfo.setIsScheduleScreen(false);
		moduleInfo.setIsTemplate(true);
		moduleInfo.setTemplateName(templateName);
		moduleInfo.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
		moduleInfo.setTitleRef(ecmsCommonDao.saveGlobalTextMap(titleMap));
		moduleInfo.setSubTitleRef(ecmsCommonDao
				.saveGlobalTextMap(subTitleMap));
		// GlobalPicture
		GlobalPicture menuPicture = new GlobalPicture();
		menuPicture.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
		menuPicture.setBinaryData(menuPictureData != null ? menuPictureData : new byte[0]);
		menuPicture.setVersion(1);
		moduleInfo.setMenuPictureRef(ecmsCommonDao.saveGlobalPicture(menuPicture).getId());
		int moduleInfoId = ecmsCommonDao.saveGlobalModuleInfo(moduleInfo);

		// Save MdlHelpMain
		MdlHelpCategory helpCat;
		int helpCatId;
		for (MdlHelpMain helpMain : helpMainList) {
			helpCat = helpMain.getHelpCategory();
			helpCat.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
			helpCat.setNameRef(ecmsCommonDao.saveGlobalTextMap(helpCat.getNameTextMap()));
			helpCatId = ecmsCommonDao.saveHelpCategory(helpCat);
			MdlHelpMainId helpMainId = new MdlHelpMainId(moduleInfoId, helpCatId);
			helpMain.setId(helpMainId);
			helpMain.setProprietaryId(saveProprietaryInfo(orgId, propId, userId));
			ecmsCommonDao.saveHelpMain(helpMain);
		}
	}

}
